(function(S) {
	/**
	 * @class S.JsonRemoteStore
	 */
	S.declare('JsonRemoteStore', S.EventTarget, {
		identifier : 'id',
		source : '',
		init : function() {
		},
		get : function(id) {
			return $.get(this.source + '/' + id);
		},
		getIdentity : function(item) {
			return item[this.identifier];
		},
		put : function(item, config) {
			config = config || {};
			return $.ajax({
				url : this.source,
				data : config,
				success : function(result) {
					self.trigger('put', {
						data : result
					});
				}
			});
		},
		add : function(item, config) {
			config = config || {};
			config.override = false;
			config.cmd = 'add';
			return this.put(item, config);
		},
		remove : function(id, config) {
			var self = this;
			config = config || {};
			config.cmd = 'remove';
			return $.post(this.source + '/' + id, config, function(result) {
				self.trigger('remove', {
					data : result
				});
			});
		},
		find : function(query) {
			var self = this, deferred;
			query = query || {};
			var postData = {};
			if (query.query || query.sorter) {
				$.extend(postData, query.query, query.sorter, {
					start : query.start || 0,
					count : query.count || 0
				});
			}
			if ( typeof query.find === 'function') {
				deferred = query.find.call(this, postData);

			} else {
				deferred = $.ajax({
					url : this.source,
					data : postData
				});
			}
			deferred.then(function(result) {
				self.trigger('find', {
					data : result
				});
			});
			return deferred;
		}
	});

})(Simple);
